package FileManager;
import Method.Conexcute;
import java.net.URLEncoder;
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet(urlPatterns = "/Downfile")
public class DownfileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String realpath =request.getParameter("str");
        String filename = realpath.substring(realpath.lastIndexOf("/")+1);
        try {
            File source=new File("D:\\picture\\"+filename);
            File dest=new File("C:\\Users\\G\\Desktop\\downfile\\"+filename);
            InputStream input = null;
            OutputStream output = null;
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            System.out.println("xxxxxxxxxxxx");
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                System.out.println("xxxxxxxxxxxx");
                output.write(buf, 0, bytesRead);
                System.out.println("xxxxxxxxxxxx");
            }
            input.close();
            output.close();
        }finally {
        }{}

        response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().println("数据下载成功");
            response.sendRedirect("FileManager/file.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}